package com.jeeplus.modules.sys.service;

import com.jeeplus.core.service.BaseService;
import com.jeeplus.modules.sys.entity.ZBHAuth;
import com.jeeplus.modules.sys.entity.ZBHuiTree;
import com.jeeplus.modules.sys.mapper.AuthTreeMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional(readOnly = true)
public class AuthService extends BaseService implements InitializingBean {


    @Autowired
    private AuthTreeMapper zBHuiTreeMapper;

    /**
     * 根据岗位id获取说在部门id
     * @param stationId
     * @return
     */
    public ZBHAuth getZBHAuth(String stationId){
        ZBHAuth zbhAuth=new ZBHAuth();
        List<String> departmentIds=new ArrayList<String>();
        this.upTreeList(zBHuiTreeMapper.findAllZBHuiTree(),stationId,zbhAuth,departmentIds);
        if(departmentIds.size()>=1){
            zbhAuth.setDepartmentId(departmentIds.get(0));
        }
        System.out.println("@@@@@@@"+zbhAuth.getCompanyId()+"#######"+zbhAuth.getDepartmentId());
        return zbhAuth;
    }

    private void upTreeList (List<ZBHuiTree> treeList, String pid, ZBHAuth zbhAuth,List<String> departmentIds) {

        for (ZBHuiTree mu : treeList) {
            // 遍历出父id等于参数的id，add进子节点集合
            if (mu.getId().equals(pid)) {
                if(mu.getOrgType().equals("1")){
                    zbhAuth.setCompanyId(mu.getId());
                }
                if(mu.getOrgType().equals("2")){
                    departmentIds.add(mu.getId());
                }
                this.upTreeList(treeList, mu.getParentId(), zbhAuth,departmentIds);
            }
        }

    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}
